package com.lineage.server.model.weaponskill;

import com.lineage.server.datatables.SkillsTable;
import com.lineage.server.model.Instance.L1ItemInstance;
import com.lineage.server.model.Instance.L1NpcInstance;
import com.lineage.server.model.Instance.L1PcInstance;
import com.lineage.server.model.L1Character;
import com.lineage.server.serverpackets.S_SkillSound;
import com.lineage.server.serverpackets.ServerBasePacket;
import java.util.Random;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

public class W_SK0012 extends L1WeaponSkillType {
  private static final Log _log = LogFactory.getLog(W_SK0012.class);
  
  private static final Random _random = new Random();
  
  public static L1WeaponSkillType get() {
    return new W_SK0012();
  }
  
  public double start_weapon_skill(L1PcInstance pc, L1Character target, L1ItemInstance weapon, double srcdmg) {
    try {
      int chance = _random.nextInt(1000);
      if (random(weapon) + pc.getWeaponSkillChance() * 10 >= chance) {
        if (target.hasSkillEffect(31)) {
          target.removeSkillEffect(31);
          int castgfx2 = SkillsTable.get().getTemplate(31).getCastGfx2();
          target.broadcastPacketAll((ServerBasePacket)new S_SkillSound(target.getId(), castgfx2));
          if (target instanceof L1PcInstance) {
            L1PcInstance tgpc = (L1PcInstance)target;
            tgpc.sendPacketsAll((ServerBasePacket)new S_SkillSound(tgpc.getId(), castgfx2));
          } 
          return 0.0D;
        } 
        if (target.hasSkillEffect(153))
          target.removeSkillEffect(153); 
        int damage = (int)(pc.getSp() * 3.8D);
        if (target.getCurrentHp() - damage < 0 && target.getCurrentHp() != 1) {
          damage = target.getCurrentHp() - 1;
        } else if (target.getCurrentHp() == 1) {
          damage = 0;
        } 
        int reMp = 5;
        int newMp = target.getCurrentMp() - 5;
        if (newMp < 0)
          newMp = 0; 
        target.setCurrentMp(newMp);
        damage += this._type1 + _random.nextInt(this._type2);
        double outdmg = dmg1() + dmg2(srcdmg) + dmg3(pc) + damage;
        outdmg = calc_dmg(pc, target, outdmg, weapon);
        if (this._type3 > 0)
          outdmg *= this._type3 / 100.0D; 
        if (pc.getWeaponSkillDmg() != 1.0D)
          outdmg += outdmg * pc.getWeaponSkillDmg(); 
        show(pc, target);
        if (target instanceof L1PcInstance) {
          L1PcInstance tgpc2 = (L1PcInstance)target;
          tgpc2.receiveDamage((L1Character)pc, outdmg, false, true);
        } else if (target instanceof L1NpcInstance) {
          L1NpcInstance npc = (L1NpcInstance)target;
          npc.receiveDamage((L1Character)pc, (int)outdmg);
        } 
      } 
      return 0.0D;
    } catch (Exception e) {
      _log.error(e.getLocalizedMessage(), e);
      return 0.0D;
    } 
  }
}
